package org.yaaic.gps_utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.yaaic.model.Channel;
import org.yaaic.model.Server;

public class ChannelCollection
{
    private static List<Channel> channelList;

    static {
        channelList = new ArrayList<Channel>();

        //        String hostDomainString = "srv.aliuy.com";
        //
        //        // CoC
        //        ChannelInfoObj info = new ChannelInfoObj("CoC", hostDomainString);
        //        LocationObj loc = new LocationObj(33.777468, -84.397344);
        //        ChannelObj chan = new ChannelObj(info, loc);
        //        channelList.add(chan);
        //
        //        // Van Leer
        //        info = new ChannelInfoObj("Van Leer", hostDomainString);
        //        loc = new LocationObj(33.775992, -84.397161);
        //        chan = new ChannelObj(info, loc);
        //        channelList.add(chan);
        //
        //        // CULC
        //        info = new ChannelInfoObj("CULC",hostDomainString);
        //        loc = new LocationObj(33.77432, -84.396453);
        //        chan = new ChannelObj(info, loc);
        //        channelList.add(chan);
        //
        //        // Library
        //        info = new ChannelInfoObj("Library", hostDomainString);
        //        loc = new LocationObj(33.774382, -84.39582);
        //        chan = new ChannelObj(info, loc);
        //        channelList.add(chan);
    }

    public static void refreshChannels() {
        channelList.clear();
        // TODO: Fix this
        for (Channel c : getDefaultChannels()){
            channelList.add(c);
        }
        //        for (Server s : Yaaic.getInstance().getServersAsArrayList()){
        //            channelList.add(new ChannelObj(s));
        //        }
    }

    public static boolean addChannel(Channel chan){
        channelList.add(chan);
        //        Yaaic.getInstance().addServer(chanObj.getServer());
        return true;
    }

    public static List<Channel> getAllChannels(){
        //TODO....FIX THIS!!
        if (channelList.isEmpty()) {
            refreshChannels();
        }
        List<Channel> rtn = new ArrayList<Channel>();
        for (Channel c : channelList) {
            rtn.add(c);
        }
        return rtn;
    }

    public static List<Channel> getChannelsByDistance(LocationObj locObj, int quantity){
        if (channelList.isEmpty()) {
            refreshChannels();
        }
        List<SortChannelObj> sorted = new ArrayList<SortChannelObj>();
        for (Channel c : channelList) {
            sorted.add(new SortChannelObj(locObj.calcDist(new LocationObj(c.getLattitude(), c.getLongitude())), c));
        }
        Collections.sort(sorted);
        List<Channel> rtn = new ArrayList<Channel>();
        for (int i=0;i < sorted.size() && i < quantity; i++) {
            SortChannelObj s = sorted.get(i);
            s.getChan().setScore(s.getScore());
            rtn.add(sorted.get(i).getChan());
        }
        return rtn;
    }

    //    public static List<Server> getServersByDistance(LocationObj locObj, int quantity) {
    //        List<Server> servers = new ArrayList<Server>();
    //        for(Channel c : getChannelsByDistance(locObj, quantity)) {
    //            servers.add(c.getServer());
    //        }
    //        return servers;
    //    }

    private static Server initServer(String title, String host, double lat, double lon) {
        Server temp = new Server();
        temp.setTitle(title);
        temp.setHost(host);
        temp.setLattitude(lat);
        temp.setLongitude(lon);
        return temp;
    }

    public static List<Server> getDefaultServers() {
        List<Server> servers = new ArrayList<Server>();

        // CoC
        servers.add(initServer("CoC", "srv.aliuy.com", 33.777468, -84.397344));

        // Van Leer
        servers.add(initServer("VanLeer", "srv.aliuy.com", 33.775992, -84.397161));

        // CULC
        servers.add(initServer("CULC", "srv.aliuy.com", 33.77432, -84.396453));

        // Library
        servers.add(initServer("Library", "srv.aliuy.com", 33.774382, -84.39582));


        //        servers.add(initServer("CoC", "srv.aliuy.com", 0.5, 0.5));
        //        servers.add(initServer("CoC", "srv.aliuy.com", 0.5, 0.5));
        //        servers.add(initServer("CoC", "srv.aliuy.com", 0.5, 0.5));

        return servers;
    }

    private static Channel initChannel(String title, String host, double lat, double lon) {
        Channel temp = new Channel(title);
        temp.setLattitude(lat);
        temp.setLongitude(lon);
        return temp;
    }

    public static List<Channel> getDefaultChannels() {
        List<Channel> servers = new ArrayList<Channel>();

        // CoC
        servers.add(initChannel("CoC", "srv.aliuy.com", 33.777468, -84.397344));

        // Van Leer
        servers.add(initChannel("VanLeer", "srv.aliuy.com", 33.775992, -84.397161));

        // CULC
        servers.add(initChannel("CULC", "srv.aliuy.com", 33.77432, -84.396453));

        // Library
        servers.add(initChannel("Library", "srv.aliuy.com", 33.774382, -84.39582));


        //        servers.add(initServer("CoC", "srv.aliuy.com", 0.5, 0.5));
        //        servers.add(initServer("CoC", "srv.aliuy.com", 0.5, 0.5));
        //        servers.add(initServer("CoC", "srv.aliuy.com", 0.5, 0.5));

        return servers;
    }
}
